package com.ytclub.oss.controller;

import com.ytclub.oss.entity.Result;
import com.ytclub.oss.service.FileService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: HuangQi
 * @date: Created in 20:06 2024/3/16
 * @description:
 */

@RestController
public class FileController {
    @Resource
    private FileService fileService;


    /**
     * 获取所有桶
     *
     * @return 桶列表
     * @throws Exception
     */
    @RequestMapping("/testCreateBucket")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }


    /**
     * 获取文件URL
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return 文件URL
     * @throws Exception
     */
    @RequestMapping("/getUrl")
    public String getUrl(String bucketName,String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     *
     * @param uploadFile 文件流
     * @param bucket     桶名称
     * @param objectName 文件名称
     * @return 文件URL
     * @throws Exception
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        return Result.ok(url);
    }


}
